package com.ext.replacedemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * 优先级：局部替换-->忽略的行-->整行替换
 * <p> contains匹配到key后，通过 = 赋值，将整行替换成value，value中的{0}可以被替换为原始行，代码会自动在尾部添加被处理过的标记。
 * <p> 如果替换后的行还包含原Key，但和value一致，是不会被二次替换的。如果不一致，为了不被二次替换，记得添加完成标记。
 */
//完成标记:让本行不再被整行替换: /*ok*/
public class Gradle_ReplaceLine {

    public ArrayList<String> sGradleKeyLineList = new ArrayList<>();
    public ArrayList<String> sGradleValueLineList = new ArrayList<>();

    private LinkedHashMap<String, String> mGradleLineHashMap = new LinkedHashMap<>();

    /*由于是整行替换，需手动添加空格对齐 */
    public Gradle_ReplaceLine(boolean migrateTo34, boolean migrateTo35, boolean migrateToX, boolean removeFacebook) {
        if (migrateTo34) {
            load34();
        }
        if (migrateTo35) {
            load35();
        }
        if (migrateToX) {
            loadAndroidX(removeFacebook);
        }
        if (mGradleLineHashMap.size() > 0) {
            Set<String> keySet = mGradleLineHashMap.keySet();
            Collection<String> values = mGradleLineHashMap.values();
            sGradleKeyLineList.addAll(Arrays.asList(keySet.toArray(new String[]{})));
            sGradleValueLineList.addAll(Arrays.asList(values.toArray(new String[]{})));
        }
    }

    private void load34() {
        mGradleLineHashMap.put("apply plugin: 'com.alibaba.arouter'", "");
        mGradleLineHashMap.put("javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }", "");

        mGradleLineHashMap.put("compileSdkVersion ", "    compileSdkVersion globalConfiguration.getAt(\"androidCompileSdkVersion\")");
        mGradleLineHashMap.put("buildToolsVersion ", "    buildToolsVersion globalConfiguration.getAt(\"androidBuildToolsVersion\")");
        mGradleLineHashMap.put("minSdkVersion ", "        minSdkVersion globalConfiguration.getAt(\"androidMinSdkVersion\")");
        mGradleLineHashMap.put("targetSdkVersion ", "        targetSdkVersion globalConfiguration.getAt(\"androidTargetSdkVersion\")");

        mGradleLineHashMap.put("dependencies {", "dependencies {/*ok*/" +
                "\n    testImplementation 'junit:junit:4.13.2'/*ok*/" +
                "\n//    api fileTree(dir: 'libs', include: ['*.jar','*.aar'])/*ok*/" +
                "\n    annotationProcessor 'com.jakewharton:butterknife:7.0.1'/*ok*/" +
                "\n    def cardappDependencies = rootProject.ext.cardappDependencies/*ok*/");

        mGradleLineHashMap.put("junit:junit:", "");
//        mGradleLineHashMap.put("fileTree(dir:", "");
        mGradleLineHashMap.put("annotationProcessor 'com.jakewharton:butterknife:", "");
        mGradleLineHashMap.put("rootProject.ext.card", "");

        mGradleLineHashMap.put("repository/maven-releases", "            url \"http://106.53.120.148:8081/repository/maven-releases/\"; allowInsecureProtocol true");
        mGradleLineHashMap.put("repository/maven-snapshots", "            url \"http://106.53.120.148:8081/repository/maven-snapshots/\"; allowInsecureProtocol true");

        mGradleLineHashMap.put("com.android.tools.build:gradle:", "        classpath 'com.android.tools.build:gradle:7.4.2'");
        mGradleLineHashMap.put("com.google.gms:google-services:", "        classpath 'com.google.gms:google-services:4.3.8'");
        mGradleLineHashMap.put("com.huawei.agconnect:agcp:", "        classpath 'com.huawei.agconnect:agcp:1.6.0.300'");
        mGradleLineHashMap.put("com.alibaba:arouter-register:", "        classpath 'io.github.JailedBird:arouter-gradle-plugin:1.0.2'");

        mGradleLineHashMap.put("androidCompileSdkVersion =", "    androidCompileSdkVersion = 34");
        mGradleLineHashMap.put("androidBuildToolsVersion =", "    androidBuildToolsVersion = \"34.0.0\"");
        mGradleLineHashMap.put("androidMinSdkVersion =", "    androidMinSdkVersion = 24");
        mGradleLineHashMap.put("androidTargetSdkVersion =", "    androidTargetSdkVersion = 34");

        mGradleLineHashMap.put("distributionUrl=", "distributionUrl=https\\://services.gradle.org/distributions/gradle-7.5-bin.zip");

        mGradleLineHashMap.put("android.enableAapt2", "");
//        mGradleLineHashMap.put("kotlin", "//kotlin {0}");
        mGradleLineHashMap.put("http://developer.huawei.com/repo", "//{0}");
    }

    private void load35() {

        mGradleLineHashMap.put("com.android.tools.build:gradle:", "        classpath 'com.android.tools.build:gradle:8.5.1'");
        mGradleLineHashMap.put("distributionUrl=", "distributionUrl=https\\://services.gradle.org/distributions/gradle-8.7-bin.zip");
        mGradleLineHashMap.put("com.huawei.agconnect:agcp:", "        classpath 'com.huawei.agconnect:agcp:1.9.4.300'");
        mGradleLineHashMap.put("classpath 'com.google.gms:google-services", "        classpath 'com.google.gms:google-services:4.4.0'");

        mGradleLineHashMap.put("androidTargetSdkVersion =", "    androidTargetSdkVersion = 35");
        mGradleLineHashMap.put(": 'com.cardapp.androidx.utils:cardappUtils:", "            cardappUtilsX           : 'com.cardapp.android15.utils:cardappUtils:1.1.0',");
    }

    private void loadAndroidX(boolean removeFacebook) {
        String value = "//{0}";
        /* 移除 */
        mGradleLineHashMap.put(".l_apkdownload", value);
        mGradleLineHashMap.put(".mobile_printer", value);

        if (removeFacebook) {
            mGradleLineHashMap.put("com.facebook.", value);
            mGradleLineHashMap.put("commonDependencies.facebook", value);
            mGradleLineHashMap.put("net.z1study.vsglue:vsglue:", value);
        }

        mGradleLineHashMap.put("com.tencent.bugly", value);
        mGradleLineHashMap.put("com.paypal.sdk:", value);

        mGradleLineHashMap.put("com.esotericsoftware.", value);
        mGradleLineHashMap.put("com.snappydb:snappydb-lib:", value);

        mGradleLineHashMap.put("com.zyp.cardview:cardview:", value);
        mGradleLineHashMap.put("com.pacioianu.david:ink-page-indicator:", value);

        mGradleLineHashMap.put("libs/umeng-common", "");
        mGradleLineHashMap.put("implementation 'com.umeng.", "");
        mGradleLineHashMap.put("api 'com.umeng.", "");
        mGradleLineHashMap.put("bintray.com/umsdk/release", "");

        mGradleLineHashMap.put("files('libs/sicpaysdk", "    implementation files('libs/sicpaysdk-abroad-1.3.0.28.aar')");

    }
}
